/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */
package autoteam.tables;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.*;
import javax.swing.table.*;

/**
 * This is a table with one row for each of the header fields read from the
 * input file. The second column is the internal field which is to be equated
 * with each header field - that is , one of the entries of DataMap.fieldNames.
 * The vector valueAt has an entry for each row (i.e. header field),
 * valueAt.get(i) is the (string form) of the DataMap.fieldNames element
 * assigned to the i-th header field.
 */

import autoteam.*;

import jxl.*;

public class ParserTableModel extends AbstractTableModel
{
    
    private static final long serialVersionUID = -4205255332244975883L;
    
    /** This is the principal object we are contstructing. */
    DataMap dataMap = null;
    
    /**
     * This holds the chosen DataMap.fieldName (as a string) for each header
     * field read from the input data file. It is a GUI object used to service
     * the table's getValueAt and setValueAt methods.
     */
    Vector valueAt = null;
    
    public ParserTableModel( File f )
    {
	dataMap = new DataMap( f );
	valueAt = new Vector( );
	dataMap.initializeMappings( valueAt );
    }
    
    public ParserTableModel( Sheet s, File f )
    {
	dataMap = new DataMap( f, s );
	valueAt = new Vector( );
	dataMap.initializeMappings( valueAt );
    }
    
    public int getColumnCount( )
    {
	return 2;
    }
    
    public int getRowCount( )
    {
	return dataMap.headerFields.length;
    }
    
    public String getColumnName( int col )
    {
	return (col == 0 ? "Field" : "Meaning");
    }
    
    public Object getValueAt( int row, int col )
    {
	if ( col == 0 )
	    return dataMap.headerFields[row];
	return valueAt.get( row );
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass( int c )
    {
	return getColumnName( 0 ).getClass( );
    }
    
    /*
     * Only first column in editable
     */
    public boolean isCellEditable( int row, int col )
    {
	return (col == 1);
    }
    
    public void setValueAt( Object value, int row, int col )
    {
	dataMap.setElement( value, row );
	valueAt.set( row, (String) value );
    }
    
    public DataMap getDataMap( )
    {
	dataMap.setSkills( valueAt );
	return dataMap;
    }
}
